package com.wk.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wk.common.bean.Resp;
import com.wk.pms.entity.Category;
import com.wk.pms.service.CategoryService;
import com.wk.pms.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据一级分类ID查询对应的二三级分类信息
     */
    @GetMapping("{pid}")
    public Resp<List<CategoryVO>> queryByPid(@PathVariable(value = "pid") Long pid) {
        List<CategoryVO> categoryVOS =categoryService.queryByPid(pid);
        return Resp.ok(categoryVOS);
    }

    /**
     * 根据父类ID或三级分类中的级别查询分类信息
     * @param level
     * @param pid
     * @return
     */
    @GetMapping
    public Resp<List<Category>> queryCategoriesByPidOrLevel(@RequestParam(value = "level",defaultValue = "0") Integer level,
                                                            @RequestParam(value = "parentCid",required = false) Long pid){
        QueryWrapper<Category> wrapper = new QueryWrapper<Category>()
                .eq(level!=0,"cat_level",level)
                .eq(pid!=null,"parent_cid",pid);
        return Resp.ok(categoryService.list(wrapper));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    public Resp<Category> getById(@PathVariable("catId") Long catId){
        Category category = categoryService.getById(catId);

        return Resp.ok(category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Resp<Object> save(@RequestBody Category category){
        categoryService.save(category);

        return Resp.ok(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Resp<Object> update(@RequestBody Category category){
        categoryService.updateById(category);

        return Resp.ok(null);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Resp<Object> delete(@RequestBody Long[] catIds){
        categoryService.removeByIds(Arrays.asList(catIds));

        return Resp.ok(null);
    }

}

